package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DevelopmentTop;
import com.ruoyi.system.domain.PopularProcess;
import com.ruoyi.system.service.IDevelopmentTopService;
import com.ruoyi.system.service.IPopularProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;

/*
* 接口类
* */
@RestController
@RequestMapping("/api")
public class ApiController extends BaseController {

    @Autowired
    private IDevelopmentTopService developmentTopService;
    @Autowired
    private IPopularProcessService popularProcessService;


    /**
     * 查询发展量排名列表
     */
    @GetMapping("/top")
    public List<DevelopmentTop> developmentTop(Long deptId)
    {
        List<DevelopmentTop> list = developmentTopService.selectDevelopmentTopListOrder(deptId);
        return list;
    }

    /**
     * 查询发展量排名列表
     */
    @GetMapping("/topDetail")
    public List<DevelopmentTop> developmentTopDetail(Long deptId,Date targetTime)
    {      // 定义输出年月日格式
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = outputFormat.format(targetTime);
        List<DevelopmentTop> list = developmentTopService.selectDevelopmentTopDetail(deptId,formattedDate);
        return list;
    }


    /**
     * 查询过程管控
     */
    @GetMapping("/popular")
    public List<PopularProcess> popularProcess(Long deptId,Date targetTime)
    {
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = outputFormat.format(targetTime);
        List<PopularProcess> list = popularProcessService.selectPopularProcessListTable(deptId,formattedDate);
        for(PopularProcess popularProcess : list){
            if (StringUtils.isNotNull(popularProcess.getSignUrl())&& StringUtils.isNotNull(popularProcess.getReplayUrl())&&
                    StringUtils.isNotNull(popularProcess.getPeakednessUrl())&&StringUtils.isNotNull(popularProcess.getSignOutUrl())){
                popularProcess.setState("1");
            }else {
                popularProcess.setState("0");
            }
        }
        return list;
    }

    /**
     * 查询实时发展量
     */
    @GetMapping("/topTotal")
    public AjaxResult topTotal(Long deptId, Date targetTime)
    {
        // 定义输出年月日格式
        SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = outputFormat.format(targetTime);
        List<Map<String, Object>> top = developmentTopService.selectDevelopmentTop(deptId,formattedDate);
        List<Map<String, Object>> total = developmentTopService.selectCommunitytotal(deptId);
        Map<String, Object> map = new HashMap<>();
        map.put("top", top);
        map.put("total", total);
        return AjaxResult.success(map);
    }
}

